package com.xxyw.youjingmall.product.service.impl;

import com.xxyw.youjingmall.product.entity.BrandEntity;
import com.xxyw.youjingmall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxyw.common.utils.PageUtils;
import com.xxyw.common.utils.Query;

import com.xxyw.youjingmall.product.dao.CategoryBrandRelationDao;
import com.xxyw.youjingmall.product.entity.CategoryBrandRelationEntity;
import com.xxyw.youjingmall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> queryPageByBrandId(Long brandId) {

        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<CategoryBrandRelationEntity>();
        wrapper.eq("brand_id", brandId);

        List<CategoryBrandRelationEntity> list = this.list(wrapper);

        return list;
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandId(brandId);
        entity.setBrandName(name);
        this.update(entity, new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId, name);
    }

    @Override
    public List<BrandEntity> selectBrandsByCatId(Long catId) {

        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<CategoryBrandRelationEntity>();
        wrapper.eq("catelog_id", catId);

        List<CategoryBrandRelationEntity> categoryBrandRelationEntityList = categoryBrandRelationDao.selectList(wrapper);

        List<BrandEntity> collect = categoryBrandRelationEntityList.stream().map((relation) -> {
            return brandService.getById(relation.getBrandId());
        }).collect(Collectors.toList());

        return collect;
    }

}